package com.zzp.completablefuturedemo;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;

public class CompletableFutureTest3 {

    //allOf  全部
    //anyOf  任意一个
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        //1.创建异步任务对象 CompletableFuture  A任务需要返回值
        CompletableFuture<Long> futureA = CompletableFuture.supplyAsync(() -> {
            try {
                Thread.sleep(10);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("A任务执行");
            return 10L;
        });

        //2.基于上面对象 构建 B任务对象
        CompletableFuture<String> futureB = futureA.thenApplyAsync((aResult) -> {
            try {
                Thread.sleep(5000);
                System.out.println("B任务执行");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return "";
        });


        //3.基于上面对象 构建 C任务对象
        CompletableFuture<String> futureC = futureA.thenApplyAsync((aResult) -> {
            try {
                Thread.sleep(11);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("C任务执行");
            return "";
        });

        //  CompletableFuture.anyOf(futureA, futureB, futureC).join();
        CompletableFuture.allOf(futureA, futureB, futureC).join();
        System.out.println("执行后续业务代码");
    }
}